﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Notoric.Gui.Controls
{
    /// <summary>
    /// Interaction logic for Section.xaml
    /// </summary>
    public partial class Section : UserControl
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="Section"/> instance.
        /// </summary>
        public Section()
        {
            InitializeComponent();
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Occurs when the user presses the left mouse button on the section.
        /// </summary>
        /// <remarks>
        /// <b>Operation:</b><br/>
        /// The handler selects the section (sets its <see cref="ViewModel.Elements.Section.IsSelected"/>
        /// property to <see langword="true"/>.
        /// </remarks>
        /// <param name="sender">The object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;

            if (Keyboard.IsKeyUp(Key.LeftCtrl) && Keyboard.IsKeyUp(Key.RightCtrl) && Keyboard.IsKeyUp(Key.LeftShift) && Keyboard.IsKeyUp(Key.RightShift))
            {
                if (section.IsSelected)
                    return;
                section.FindOwner<ViewModel.Elements.Page>().ClearSelection();
            }
            else if (section.IsSelected)
            {
                section.FindOwner<ViewModel.Elements.Page>().RemoveFromSelection(section);
                return;
            }

            section.FindOwner<ViewModel.Elements.Page>().SelectSection(section);
        }

        /// <summary>
        /// Occurs when the user has finished moving the section.
        /// </summary>
        /// <remarks>
        /// <b>Operation:</b><br/>
        /// The handler commits the changes made to the section.
        /// </remarks>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void RRMDecorator_MoveCompleted(object sender, Adorners.MoveCompletedEventArgs e)
        {
            section.CommitChanges();
        }

        /// <summary>
        /// Occurs when the user has finished resizing the section.
        /// </summary>
        /// <remarks>
        /// <b>Operation:</b><br/>
        /// The handler commits the changes made to the section.
        /// </remarks>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void RRMDecorator_ResizeCompleted(object sender, Adorners.ResizeCompletedEventArgs e)
        {
            section.CommitChanges();
        }

        /// <summary>
        /// Occurs when the user has finished rotating the section.
        /// </summary>
        /// <remarks>
        /// <b>Operation:</b><br/>
        /// The handler commits the changes made to the section.
        /// </remarks>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void RRMDecorator_RotateCompleted(object sender, Adorners.RotateCompletedEventArgs e)
        {
            section.CommitChanges();
        }

        /// <summary>
        /// Occurs when the data context (visualized section) of this control has changed.
        /// </summary>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            section = (ViewModel.Elements.Section)DataContext;
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// Holds the visualized section.
        /// </summary>
        private ViewModel.Elements.Section section;

        #endregion
    }
}
